package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.service.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.BrandMapper;
import com.atguigu.spzx.manager.service.BrandServcie;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BrandServcieImpl implements BrandServcie {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Brand> findAllByPage(Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        List<Brand> list = brandMapper.findALl();
        PageInfo<Brand> brandPageInfo = new PageInfo<>(list);
        return brandPageInfo;
    }

    @Override
    public void addBrand(Brand brand) {
        if(brand.getName()==null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        brandMapper.insertBrand(brand);
    }

    @Override
    public void updateById(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public Brand getById(Long id) {
        Brand brand = brandMapper.selectById(id);
        return brand;
    }

    @Override
    public void deleteById(Long id) {
        brandMapper.deleteById(id);
    }

    @Override
    public List<Brand> findAll() {
        List<Brand> brandList=brandMapper.findALl();
        return brandList;
    }
}
